﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmMaintainProductionType : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        public MstProductTypeEntity SelectedProductType { get; set; }
        #endregion

        #region ctors
        public frmMaintainProductionType()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCategory, "CG", true);
        }
        private void SetFields()
        {
            if (SelectedProductType == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(SelectedProductType.ParentTypeId) == false)
            {
                MstProductTypeEntity parent = MstProductTypeBs.GetMstProductTypeByID(SelectedProductType.ParentTypeId);
                txtParentType.Text = parent.ProductTypeName;
                txtParentType.Tag = parent;
            }
            txtTypeCode.Text = SelectedProductType.ProductTypeCode;
            txtTypeName.Text = SelectedProductType.ProductTypeName;
            cmbCategory.SetSelectedValue(SelectedProductType.Category);
            txtDescription.Text = SelectedProductType.Description;
        }
        private void SetEntity()
        {
            if (SelectedProductType == null)
            {
                SelectedProductType = new MstProductTypeEntity();
                SelectedProductType.ProductTypeId = Guid.NewGuid().ToString();
            }
            SelectedProductType.ParentTypeId = null;
            if (txtParentType.Tag != null && txtParentType.Tag.GetType() == typeof(MstProductTypeEntity))
            {
                MstProductTypeEntity parent = (MstProductTypeEntity)txtParentType.Tag;
                SelectedProductType.ParentTypeId = parent.ProductTypeId;
            }
            SelectedProductType.ProductTypeCode = txtTypeCode.Text;
            SelectedProductType.ProductTypeName = txtTypeName.Text;
            if (cmbCategory.SelectedValue != null && string.IsNullOrEmpty(cmbCategory.SelectedValue.ToString()) == false)
            {
                SelectedProductType.Category = cmbCategory.SelectedValue.ToString();
            }
            SelectedProductType.Description = txtDescription.Text;
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtTypeCode.Text))
            {
                MessageBox.Show("类别编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtTypeName.Text))
            {
                MessageBox.Show("类别名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbCategory.Text))
            {
                MessageBox.Show("分类不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = MstProductTypeBs.InsertMstProductType(SelectedProductType);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstProductTypeBs.UpdateMstProductType(SelectedProductType);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetFields();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        private void txtParentType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductionTypeQuery frm = new frmMaintainProductionTypeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProductType != null)
            {
                txtParentType.Text = frm.SelectedProductType.ProductTypeName + " (" + frm.SelectedProductType.ProductTypeCode + ")";
                txtParentType.Tag = frm.SelectedProductType;
            }
            else
            {
                txtParentType.Text = "";
                txtParentType.Tag = null;
            }
        }
        #endregion
    }
}
